<!DOCTYPE html>


  <html class="dark page-post">


<head><meta name="generator" content="Hexo 3.9.0">
  <meta charset="utf-8">
  
  <title>JavaScript数组、字符串、对象常用方法 | Poetry&#39;s Blog</title>

  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">

  
    <meta name="keywords" content="JavaScript,API,">
  

  <meta name="description" content="一、String对象1.1 slice stringObject.slice(start, end)  var a = &apos;Hello world!&apos;;var b = a.slice(2);var c = a.slice(-4, -2);// a: &apos;Hello world!&apos;// b: &apos;llo world!&apos;// c: &apos;rl&apos;，参数可为负 1.2 substr stringObject.sub">
<meta name="keywords" content="JavaScript,API">
<meta property="og:type" content="article">
<meta property="og:title" content="JavaScript数组、字符串、对象常用方法">
<meta property="og:url" content="http://blog.poetries.top/2018/02/23/js-string-arr-object-api/index.html">
<meta property="og:site_name" content="Poetry&#39;s Blog">
<meta property="og:description" content="一、String对象1.1 slice stringObject.slice(start, end)  var a = &apos;Hello world!&apos;;var b = a.slice(2);var c = a.slice(-4, -2);// a: &apos;Hello world!&apos;// b: &apos;llo world!&apos;// c: &apos;rl&apos;，参数可为负 1.2 substr stringObject.sub">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2020-08-15T04:25:31.926Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="JavaScript数组、字符串、对象常用方法">
<meta name="twitter:description" content="一、String对象1.1 slice stringObject.slice(start, end)  var a = &apos;Hello world!&apos;;var b = a.slice(2);var c = a.slice(-4, -2);// a: &apos;Hello world!&apos;// b: &apos;llo world!&apos;// c: &apos;rl&apos;，参数可为负 1.2 substr stringObject.sub">

  

  
    <link rel="icon" href="/favicon.ico">
  

  <link href="/css/styles.css?v=c114cbeddx" rel="stylesheet">
<link href="/css/other.css?v=c114cbeddx" rel="stylesheet">


  
    <link rel="stylesheet" href="/css/personal-style.css">
  

  

  
  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "//hm.baidu.com/hm.js?40b1f89aa80f2527b3db779c6898c879";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>


  
  <script type="text/javascript">
	(function(){
	    var bp = document.createElement('script');
	    var curProtocol = window.location.protocol.split(':')[0];
	    if (curProtocol === 'https') {
	        bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';        
	    }
	    else {
	        bp.src = 'http://push.zhanzhang.baidu.com/push.js';
	    }
	    var s = document.getElementsByTagName("script")[0];
	    s.parentNode.insertBefore(bp, s);
	})();
  </script>



  
    <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
    <link rel="stylesheet" href="//cdn.bootcss.com/font-awesome/4.3.0/css/font-awesome.min.css">
  

  <!-- 聊天系统 -->
  
    
   <link type="text/css" rel="stylesheet" href="/renxi/default.css">
   <style>
      #modal {
        position: static !important;
      }
      .filter {
        width: 100%;
        height: 100%;
        position: absolute;
        top: 0;
        left: 0;
        background: #fe5757;
        animation: colorChange 30s ease-in-out infinite;
        animation-fill-mode: both;
        mix-blend-mode: overlay;
      }
  
      @keyframes colorChange {
        0%, 100% {
            opacity: 0;
        }
        50% {
            opacity: .9;
        }
      }
   </style>
</head>
</html>
<body>
  
  
    <span id="toolbox-mobile" class="toolbox-mobile">导航</span>
  

  <div class="post-header CENTER">
   
  <div class="toolbox">
    <a class="toolbox-entry" href="/">
      <span class="toolbox-entry-text">导航</span>
      <i class="icon-angle-down"></i>
      <i class="icon-home"></i>
    </a>
    <ul class="list-toolbox">
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/archives/"
            rel="noopener noreferrer"
            target="_self"
            >
            博客
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/categories/"
            rel="noopener noreferrer"
            target="_self"
            >
            分类
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/tags/"
            rel="noopener noreferrer"
            target="_self"
            >
            标签
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/search/"
            rel="noopener noreferrer"
            target="_self"
            >
            搜索
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/link/"
            rel="noopener noreferrer"
            target="_self"
            >
            友链
          </a>
        </li>
      
        <li class="item-toolbox">
          <a
            class="CIRCLE"
            href="/about/"
            rel="noopener noreferrer"
            target="_self"
            >
            关于
          </a>
        </li>
      
    </ul>
  </div>


</div>


  <div id="toc" class="toc-article">
    <strong class="toc-title">文章目录<i class="iconfont toc-title" style="display:inline-block;color:#87998d;width:20px;height:20px;">&#xf004b;</i></strong>
    <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#一、String对象"><span class="toc-text">一、String对象</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1-1-slice"><span class="toc-text">1.1 slice</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#1-2-substr"><span class="toc-text">1.2 substr</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#1-3-substring"><span class="toc-text">1.3 substring</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#二、Array对象"><span class="toc-text">二、Array对象</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#2-1-concat"><span class="toc-text">2.1 concat</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-2-pop"><span class="toc-text">2.2 pop</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-3-push"><span class="toc-text">2.3 push</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-4-shift"><span class="toc-text">2.4 shift</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-5-unshift"><span class="toc-text">2.5 unshift</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-6-slice"><span class="toc-text">2.6 slice</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-7-splice"><span class="toc-text">2.7 splice</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-8-sort"><span class="toc-text">2.8 sort</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-9-reverse"><span class="toc-text">2.9 reverse</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-10-map"><span class="toc-text">2.10 map</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-11-forEach"><span class="toc-text">2.11 forEach</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-12-find"><span class="toc-text">2.12 find</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-13-filter"><span class="toc-text">2.13 filter</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-14-reduce和reduceRight"><span class="toc-text">2.14 reduce和reduceRight</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-15-some"><span class="toc-text">2.15 some</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-16-every"><span class="toc-text">2.16 every</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#三、Object"><span class="toc-text">三、Object</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#3-1-for-in"><span class="toc-text">3.1 for in</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-2-for-of"><span class="toc-text">3.2 for of</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#3-2-1-entries"><span class="toc-text">3.2.1 entries</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-2-2-keys"><span class="toc-text">3.2.2 keys</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-2-3-values"><span class="toc-text">3.2.3 values</span></a></li></ol></li></ol></li></ol>
  </div>
  




<div class="content content-post CENTER">
   <!-- canvas 彩带 -->
<canvas id="evanyou" width="1302" height="678" style="position: fixed;width: 100%;height: 100%;top: 0;left:0;z-index:-1;"></canvas>

<article id="post-js-string-arr-object-api" class="article article-type-post" itemprop="blogPost">
  <header class="article-header" style="position:relative;">
    <h1 class="post-title">JavaScript数组、字符串、对象常用方法</h1>

    <div class="article-meta">
      <span>
        <i class="icon-calendar"></i>
        <span>2018.02.23</span>
      </span>

      
        <span class="article-author">
          <i class="icon-user"></i>
          <span>Poetry</span>
        </span>
      

      
  <span class="article-category">
    <i class="icon-list"></i>
    <a class="article-category-link" href="/categories/Front-End/">Front-End</a>
  </span>



      

      
      <i class="fa fa-eye"></i> 
        <span id="busuanzi_container_page_pv">
           &nbsp热度 <span id="busuanzi_value_page_pv">
           <i class="fa fa-spinner fa-spin"></i></span>℃
        </span>
      
      
       
          <span class="post-count">
            <i class="fa fa-file-word-o"></i>&nbsp
            <span>字数统计 2.2k字</span>
          </span>

          <span class="post-count">
            <i class="fa fa-columns"></i>&nbsp
            <span>阅读时长 10分</span>
          </span>
      
      
    </div>

    <i class="iconfont" id="toc-eye" style="display:inline-block;color:#b36619;position:absolute;top:0;right:0;cursor:pointer;
    font-size: 24px;">&#xe61c;</i>

  </header>

  <div class="article-content">
    
      <div id="container">
        <h2 id="一、String对象"><a href="#一、String对象" class="headerlink" title="一、String对象"></a>一、String对象</h2><h3 id="1-1-slice"><a href="#1-1-slice" class="headerlink" title="1.1 slice"></a>1.1 slice</h3><ul>
<li><code>stringObject.slice(start, end)</code></li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> a = <span class="string">'Hello world!'</span>;</span><br><span class="line"><span class="keyword">var</span> b = a.slice(<span class="number">2</span>);</span><br><span class="line"><span class="keyword">var</span> c = a.slice(<span class="number">-4</span>, <span class="number">-2</span>);</span><br><span class="line"><span class="comment">// a: 'Hello world!'</span></span><br><span class="line"><span class="comment">// b: 'llo world!'</span></span><br><span class="line"><span class="comment">// c: 'rl'，参数可为负</span></span><br></pre></td></tr></table></figure>
<h3 id="1-2-substr"><a href="#1-2-substr" class="headerlink" title="1.2 substr"></a>1.2 substr</h3><ul>
<li><code>stringObject.substr(start, length)</code></li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> a = <span class="string">'Hello world!'</span>;</span><br><span class="line"><span class="keyword">var</span> b = a.substr(<span class="number">0</span>, <span class="number">4</span>);</span><br><span class="line"><span class="keyword">var</span> c = a.substr(<span class="number">-5</span>, <span class="number">2</span>);</span><br><span class="line"><span class="comment">// a: 'Hello world!'</span></span><br><span class="line"><span class="comment">// b: 'Hell'</span></span><br><span class="line"><span class="comment">// c: 'or'，参数可为负</span></span><br></pre></td></tr></table></figure>
<h3 id="1-3-substring"><a href="#1-3-substring" class="headerlink" title="1.3 substring"></a>1.3 substring</h3><ul>
<li><code>stringObject.substring(start, stop)</code></li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> a = <span class="string">'Hello world!'</span>;</span><br><span class="line"><span class="keyword">var</span> b = a.substring(<span class="number">0</span>, <span class="number">4</span>);</span><br><span class="line"><span class="keyword">var</span> c = a.substring(<span class="number">3</span>, <span class="number">2</span>);</span><br><span class="line"><span class="keyword">var</span> d = a.substring(<span class="number">0</span>, <span class="number">-1</span>);</span><br><span class="line"><span class="comment">// a: 'Hello world!'</span></span><br><span class="line"><span class="comment">// b: 'Hell'</span></span><br><span class="line"><span class="comment">// c: 'l'，start比stop小，交换这两个参数</span></span><br><span class="line"><span class="comment">// d: ''，参数为负，返回空字符串</span></span><br></pre></td></tr></table></figure>
<blockquote>
<p><code>slice</code>、<code>substr</code>、<code>substring</code>都是字符串的切割方法，三者之间有细微的区别，根据不同的使用场景可以灵活使用。三种方法都是生成新的字符串，而不是修改原<code>string</code></p>
</blockquote>
<h2 id="二、Array对象"><a href="#二、Array对象" class="headerlink" title="二、Array对象"></a>二、Array对象</h2><h3 id="2-1-concat"><a href="#2-1-concat" class="headerlink" title="2.1 concat"></a>2.1 concat</h3><ul>
<li>参数可以为具体的值，也可以为数组对象，可以任意多个。不改变现有的数组，返回被连接数组的一个副本。</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> a = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>];</span><br><span class="line"><span class="keyword">var</span> b = a.concat(<span class="number">4</span>, <span class="number">5</span>);</span><br><span class="line"><span class="keyword">var</span> c = a.concat([<span class="number">4</span>, <span class="number">5</span>]);</span><br><span class="line"><span class="comment">// a: [1, 2, 3]</span></span><br><span class="line"><span class="comment">// b: [1, 2, 3, 4, 5]</span></span><br><span class="line"><span class="comment">// c: [1, 2, 3, 4, 5]</span></span><br></pre></td></tr></table></figure>
<h3 id="2-2-pop"><a href="#2-2-pop" class="headerlink" title="2.2 pop"></a>2.2 pop</h3><ul>
<li>删除 <code>arrayObject</code> 的最后一个元素，把数组长度减 <code>1</code>，并且返回它删除的元素的值。如果数组已经为空，则 pop() 不改变数组，并返回 <code>undefined</code> 值。该方法会改变原数组</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> a = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>];</span><br><span class="line"><span class="keyword">var</span> b = a.pop();</span><br><span class="line"><span class="comment">// a: [1, 2]，修改了原数组</span></span><br><span class="line"><span class="comment">// b: 3，返回删除元素的值</span></span><br></pre></td></tr></table></figure>
<h3 id="2-3-push"><a href="#2-3-push" class="headerlink" title="2.3 push"></a>2.3 push</h3><ul>
<li>参数顺序添加到 <code>arrayObject</code> 的尾部，直接修改<code>arrayObject</code></li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> a = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>];</span><br><span class="line"><span class="keyword">var</span> b = a.push(<span class="number">4</span>, <span class="number">5</span>);</span><br><span class="line"><span class="comment">// a: [1, 2, 3, 4, 5]，修改了原数组</span></span><br><span class="line"><span class="comment">// b: 5，返回修改后的数组的长度</span></span><br></pre></td></tr></table></figure>
<h3 id="2-4-shift"><a href="#2-4-shift" class="headerlink" title="2.4 shift"></a>2.4 shift</h3><ul>
<li>把数组的第一个元素从其中删除，并返回第一个元素的值。如果数组是空的，那么 <code>shift()</code> 方法将不进行任何操作，返回<code>undefined</code>值。该方法会改变原数组。类比<code>pop</code>方法</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> a = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>];</span><br><span class="line"><span class="keyword">var</span> b = a.shift();</span><br><span class="line"><span class="comment">// a: [2, 3]，修改了原数组</span></span><br><span class="line"><span class="comment">// b: 1，返回删除元素的值</span></span><br></pre></td></tr></table></figure>
<h3 id="2-5-unshift"><a href="#2-5-unshift" class="headerlink" title="2.5 unshift"></a>2.5 unshift</h3><ul>
<li>向数组的开头添加一个或更多元素，并返回新的长度。该方法的第一个参数将成为数组的新元素 <code>0</code>，如果还有第二个参数，它将成为新的元素 <code>1</code>，以此类推</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> a = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>];</span><br><span class="line"><span class="keyword">var</span> b = a.unshift(<span class="number">4</span>, <span class="number">5</span>);</span><br><span class="line"><span class="comment">// a: [ 4, 5, 1, 2, 3 ]，修改了原数组</span></span><br><span class="line"><span class="comment">// b: 5，返回修改后的数组的长度</span></span><br></pre></td></tr></table></figure>
<h3 id="2-6-slice"><a href="#2-6-slice" class="headerlink" title="2.6 slice"></a>2.6 slice</h3><ul>
<li>返回一个新的数组，包含从 <code>start</code> 到 <code>end</code> （不包括该元素）的 <code>arrayObject</code> 中的元素。该方法不会修改原数组</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> a = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>];</span><br><span class="line"><span class="keyword">var</span> b = a.slice(<span class="number">2</span>);</span><br><span class="line"><span class="comment">// a: [1, 2, 3, 4, 5]，不修改原数组</span></span><br><span class="line"><span class="comment">// b: [3, 4, 5]，返回新数组</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> c = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>];</span><br><span class="line"><span class="keyword">var</span> d = c.slice(<span class="number">2</span>, <span class="number">-1</span>);</span><br><span class="line"><span class="comment">// c: [1, 2, 3, 4, 5]，不修改原数组</span></span><br><span class="line"><span class="comment">// d: [3, 4]，返回新数组</span></span><br></pre></td></tr></table></figure>
<h3 id="2-7-splice"><a href="#2-7-splice" class="headerlink" title="2.7 splice"></a>2.7 splice</h3><ul>
<li>可删除从 <code>index</code> 处开始的零个或多个元素，并且用参数列表中声明的一个或多个值来替换那些被删除的元素。如果从 <code>arrayObject</code> 中删除了元素，则返回的是含有被删除的元素的数组。</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> a = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>];</span><br><span class="line"><span class="keyword">var</span> b = a.splice(<span class="number">1</span>, <span class="number">1</span>);</span><br><span class="line"><span class="comment">// a: [1, 3, 4, 5]，修改了原数组</span></span><br><span class="line"><span class="comment">// b: [2]，返回新数组</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> c = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>];</span><br><span class="line"><span class="keyword">var</span> d = c.splice(<span class="number">-1</span>, <span class="number">1</span>);</span><br><span class="line"><span class="comment">// c: [1, 2, 3, 4]，修改了原数组</span></span><br><span class="line"><span class="comment">// d: [5]，返回新数组</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> e = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>];</span><br><span class="line"><span class="keyword">var</span> f = e.splice(<span class="number">1</span>, <span class="number">1</span>, <span class="number">6</span>, <span class="number">7</span>);</span><br><span class="line"><span class="comment">// e: [ 1, 6, 7, 3, 4, 5 ]，修改了原数组</span></span><br><span class="line"><span class="comment">// f: [2]，返回新数组</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">var</span> g = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>];</span><br><span class="line"><span class="keyword">var</span> h = g.splice(<span class="number">1</span>, <span class="number">0</span>, <span class="number">8</span>);</span><br><span class="line"><span class="comment">// g: [ 1, 8, 2, 3, 4, 5 ]，修改了原数组</span></span><br><span class="line"><span class="comment">// h: []，没有删除值，返回空数组</span></span><br></pre></td></tr></table></figure>
<h3 id="2-8-sort"><a href="#2-8-sort" class="headerlink" title="2.8 sort"></a>2.8 sort</h3><ul>
<li>无参数时，将按字母顺序对数组中的元素进行排序。参数为比较函数时，如果要交换<code>prev</code>和<code>next</code>的值，返回大于<code>0</code>的值</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> a = [<span class="number">1</span>, <span class="number">10</span>, <span class="number">8</span>, <span class="number">6</span>, <span class="number">9</span>];</span><br><span class="line"><span class="keyword">var</span> b = a.sort(<span class="function"><span class="keyword">function</span> (<span class="params">prev, next</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> prev - next;</span><br><span class="line">&#125;);</span><br><span class="line"><span class="comment">// a: [1, 6, 8, 9, 10]，修改了原数组</span></span><br><span class="line"><span class="comment">// b: [1, 6, 8, 9, 10]，返回修改后的数组</span></span><br></pre></td></tr></table></figure>
<h3 id="2-9-reverse"><a href="#2-9-reverse" class="headerlink" title="2.9 reverse"></a>2.9 reverse</h3><ul>
<li>用于颠倒数组中元素的顺序。会改变原数组</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> a = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>];</span><br><span class="line"><span class="keyword">var</span> b = a.reverse();</span><br><span class="line"><span class="comment">// a: [3, 2, 1]，修改了原数组</span></span><br><span class="line"><span class="comment">// b: [3, 2, 1]，返回修改后的数组</span></span><br></pre></td></tr></table></figure>
<h3 id="2-10-map"><a href="#2-10-map" class="headerlink" title="2.10 map"></a>2.10 map</h3><blockquote>
<p>有返回值，返回一个新的数组，每个元素为调用<code>func</code>的结果</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">let</span> list = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>];</span><br><span class="line"><span class="keyword">let</span> other = list.map(<span class="function">(<span class="params">d, i</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">return</span> d * <span class="number">2</span>;</span><br><span class="line">&#125;);</span><br><span class="line"><span class="built_in">console</span>.log(other);</span><br><span class="line"><span class="comment">// print: [2, 4, 6, 8, 10]</span></span><br></pre></td></tr></table></figure>
<h3 id="2-11-forEach"><a href="#2-11-forEach" class="headerlink" title="2.11 forEach"></a>2.11 forEach</h3><ul>
<li>数组的每个元素执行一次提供的函数。一般来说不修改原数组，但也可以通过处理函数修改原数组。该方法很灵活，可类比<code>for...of</code></li>
<li>没有返回值，只针对每个元素调用<code>func</code>。</li>
<li>优点：代码简介。</li>
<li>缺点：无法使用<code>break</code>，<code>return</code>等终止循环</li>
</ul>
<blockquote>
<ul>
<li><code>value</code> 当前操作的数组元素</li>
<li>当前操作元素的数组索引</li>
<li><code>array</code> 当前数组的引用</li>
</ul>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">let</span> list = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>];</span><br><span class="line">list.forEach(<span class="function">(<span class="params">d, i</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="keyword">this</span>.push(d * <span class="number">2</span>);</span><br><span class="line">&#125;);</span><br><span class="line"><span class="built_in">console</span>.log(other);</span><br><span class="line"><span class="comment">// print: [2, 4, 6, 8, 10]</span></span><br></pre></td></tr></table></figure>
<h3 id="2-12-find"><a href="#2-12-find" class="headerlink" title="2.12 find"></a>2.12 find</h3><ul>
<li>返回数组中<strong>第一个满足测试条件</strong>（返回<code>true</code>）的元素。如果不存在这样的元素，返回<code>undefined</code>。<code>findIndex</code>类似，只不过返回的是第一个满足测试条件的元素的<code>index</code></li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> a = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>];</span><br><span class="line"><span class="keyword">var</span> b = a.find(<span class="function">(<span class="params">curVal</span>) =&gt;</span> curVal === <span class="number">1</span>);</span><br><span class="line"><span class="keyword">var</span> c = a.find(<span class="function">(<span class="params">curVal</span>) =&gt;</span> curVal === <span class="number">4</span>);</span><br><span class="line"><span class="comment">// a: [1, 2, 3]，不修改原数组</span></span><br><span class="line"><span class="comment">// b: 1</span></span><br><span class="line"><span class="comment">// c: undefined</span></span><br></pre></td></tr></table></figure>
<h3 id="2-13-filter"><a href="#2-13-filter" class="headerlink" title="2.13 filter"></a>2.13 filter</h3><ul>
<li>返回数组中<strong>所有满足测试条件</strong>（返回<code>true</code>）的元素组成的数组。如果不存在这样的元素，返回<code>[]</code></li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> a = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>];</span><br><span class="line"><span class="keyword">var</span> b = a.filter(<span class="function">(<span class="params">curVal</span>) =&gt;</span> curVal &gt; <span class="number">1</span>);</span><br><span class="line"><span class="keyword">var</span> c = a.filter(<span class="function">(<span class="params">curVal</span>) =&gt;</span> curVal &gt; <span class="number">3</span>);</span><br><span class="line"><span class="comment">// a: [1, 2, 3]，不修改原数组</span></span><br><span class="line"><span class="comment">// b: [2, 3]</span></span><br><span class="line"><span class="comment">// c: []</span></span><br></pre></td></tr></table></figure>
<h3 id="2-14-reduce和reduceRight"><a href="#2-14-reduce和reduceRight" class="headerlink" title="2.14 reduce和reduceRight"></a>2.14 reduce和reduceRight</h3><blockquote>
<p><code>.reduce</code>从左到右而<code>.reduceRight</code>从右到左循环遍历数组，每次调用接收目前为止的部分结果和当前遍历的值</p>
</blockquote>
<ul>
<li>两种方法都有如下典型用法：<code>.reduce(callback(previousValue, currentValue, index, array), initialValue)</code>。</li>
<li><code>previousValue</code>是最后被调用的回调函数的返回值，<code>initialValue</code>是开始时<code>previousValue</code>被初始化的值。<code>currentValue</code></li>
<li>是当前被遍历的元素值，<code>index</code>是当前元素在数组中的索引值。<code>array</code>是对调用<code>.reduce</code>数组的简单引用</li>
</ul>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="built_in">Array</span>.prototype.sum = <span class="function"><span class="keyword">function</span> (<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">this</span>.reduce(<span class="function"><span class="keyword">function</span> (<span class="params">partial, value</span>) </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> partial + value</span><br><span class="line">    &#125;, <span class="number">0</span>)</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line">[<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>,<span class="number">6</span>,<span class="number">10</span>].sum()</span><br><span class="line"><span class="comment">// &lt;- 28</span></span><br></pre></td></tr></table></figure>
<blockquote>
<p>可以使用<code>.reduce</code>作为对象的字符串生成器</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">concat</span> (<span class="params">input</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> input.reduce(<span class="function"><span class="keyword">function</span> (<span class="params">partial, value</span>) </span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (partial) &#123;</span><br><span class="line">            partial += <span class="string">', '</span></span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> partial + value</span><br><span class="line">    &#125;, <span class="string">''</span>)</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line">concat([</span><br><span class="line">    &#123; <span class="attr">name</span>: <span class="string">'George'</span> &#125;,</span><br><span class="line">    &#123; <span class="attr">name</span>: <span class="string">'Sam'</span> &#125;,</span><br><span class="line">    &#123; <span class="attr">name</span>: <span class="string">'Pear'</span> &#125;</span><br><span class="line">])</span><br><span class="line"><span class="comment">// &lt;- 'George, Sam, Pear'</span></span><br></pre></td></tr></table></figure>
<h3 id="2-15-some"><a href="#2-15-some" class="headerlink" title="2.15 some"></a>2.15 some</h3><blockquote>
<p>返回一个<code>boolean</code>，判断是否有元素符合<code>func</code>条件，如果有一个元素符合<code>func</code>条件，则循环会终止</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">let</span> list = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>];</span><br><span class="line">list.some(<span class="function">(<span class="params">d, i</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(d, i);</span><br><span class="line">    <span class="keyword">return</span> d &gt; <span class="number">3</span>;</span><br><span class="line">&#125;);</span><br><span class="line"><span class="comment">// print: 1,0 2,1 3,2 4,3</span></span><br><span class="line"><span class="comment">// return false</span></span><br></pre></td></tr></table></figure>
<h3 id="2-16-every"><a href="#2-16-every" class="headerlink" title="2.16 every"></a>2.16 every</h3><blockquote>
<p>返回一个<code>boolean</code>，判断每个元素是否符合<code>func</code>条件，有一个元素不满足<code>func</code>条件，则循环终止，返回<code>false</code></p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">let</span> list = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>];</span><br><span class="line">list.every(<span class="function">(<span class="params">d, i</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(d, i);</span><br><span class="line">    <span class="keyword">return</span> d &lt; <span class="number">3</span>;</span><br><span class="line">&#125;);</span><br><span class="line"><span class="comment">// print: 1,0 2,1 3,2</span></span><br><span class="line"><span class="comment">// return false</span></span><br></pre></td></tr></table></figure>
<h2 id="三、Object"><a href="#三、Object" class="headerlink" title="三、Object"></a>三、Object</h2><h3 id="3-1-for-in"><a href="#3-1-for-in" class="headerlink" title="3.1 for in"></a>3.1 for in</h3><blockquote>
<p><code>for-in</code>循环实际是为循环”enumerable“对象而设计的，<code>for in</code>也可以循环数组，但是不推荐这样使用，<code>for–in</code>是用来循环带有字符串<code>key</code>的对象的方法</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">var</span> obj = &#123;<span class="attr">a</span>:<span class="number">1</span>, <span class="attr">b</span>:<span class="number">2</span>, <span class="attr">c</span>:<span class="number">3</span>&#125;;</span><br><span class="line"><span class="keyword">for</span> (<span class="keyword">var</span> prop <span class="keyword">in</span> obj) &#123;</span><br><span class="line">  <span class="built_in">console</span>.log(<span class="string">"obj."</span> + prop + <span class="string">" = "</span> + obj[prop]);</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// print:  "obj.a = 1" "obj.b = 2" "obj.c = 3"</span></span><br></pre></td></tr></table></figure>
<h3 id="3-2-for-of"><a href="#3-2-for-of" class="headerlink" title="3.2 for of"></a>3.2 for of</h3><blockquote>
<p><code>for of</code>为<code>ES6</code>提供，具有<code>iterator</code>接口，就可以用<code>for of</code>循环遍历它的成员</p>
</blockquote>
<ul>
<li><code>for of</code>循环可以使用的范围包括数组、<code>Set</code>和<code>Map</code>结构、某些类似数组的对象（比如<code>arguments</code>对象、<code>DOM NodeList</code>对象）、后文的<code>Generator</code>对象，以及字符串</li>
</ul>
<h4 id="3-2-1-entries"><a href="#3-2-1-entries" class="headerlink" title="3.2.1 entries"></a>3.2.1 entries</h4><blockquote>
<p><code>entries()</code> 返回一个遍历器对象，用来遍历[键名, 键值]组成的数组。对于数组，键名就是索引值；对于<code>Set</code>，键名与键值相同。<code>Map</code>结构的<code>iterator</code>接口，默认就是调用<code>entries</code>方法</p>
</blockquote>
<h4 id="3-2-2-keys"><a href="#3-2-2-keys" class="headerlink" title="3.2.2 keys"></a>3.2.2 keys</h4><ul>
<li><code>keys()</code> 返回一个遍历器对象，用来遍历所有的键名。</li>
</ul>
<h4 id="3-2-3-values"><a href="#3-2-3-values" class="headerlink" title="3.2.3 values"></a>3.2.3 values</h4><ul>
<li><code>values()</code>返回一个遍历器对象，用来遍历所有的键值。 </li>
</ul>
<blockquote>
<p>这三个方法调用后生成的遍历器对象，所遍历的都是计算生成的数据结构</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="code"><pre><span class="line"><span class="comment">// 遍历数组</span></span><br><span class="line"><span class="keyword">let</span> list = [<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>, <span class="number">4</span>, <span class="number">5</span>];</span><br><span class="line"><span class="keyword">for</span> (<span class="keyword">let</span> e <span class="keyword">of</span> list) &#123;</span><br><span class="line">    <span class="built_in">console</span>.log(e);</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// print: 1 2 3 4 5</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 遍历对象</span></span><br><span class="line">obj = &#123;<span class="attr">a</span>:<span class="number">1</span>, <span class="attr">b</span>:<span class="number">2</span>, <span class="attr">c</span>:<span class="number">3</span>&#125;;</span><br><span class="line"><span class="keyword">for</span> (<span class="keyword">let</span> key <span class="keyword">of</span> <span class="built_in">Object</span>.keys(obj)) &#123;</span><br><span class="line">  <span class="built_in">console</span>.log(key, obj[key]);</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// print:  a 1 b 2 c 3</span></span><br><span class="line"><span class="comment">//说明：对于普通的对象，for...in循环可以遍历键名，for...of循环会报错。</span></span><br><span class="line"><span class="comment">//一种解决方法是，使用Object.keys方法将对象的键名生成一个数组，然后遍历这个数组。</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// entries</span></span><br><span class="line"><span class="keyword">let</span> arr = [<span class="string">'a'</span>, <span class="string">'b'</span>, <span class="string">'c'</span>];</span><br><span class="line"><span class="keyword">for</span> (<span class="keyword">let</span> pair <span class="keyword">of</span> arr.entries()) &#123;</span><br><span class="line">  <span class="built_in">console</span>.log(pair);</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// [0, 'a']</span></span><br><span class="line"><span class="comment">// [1, 'b']</span></span><br><span class="line"><span class="comment">// [2, 'c']</span></span><br></pre></td></tr></table></figure>

      </div>
    
  </div>

</article>

<button class="assist-btn2 circle" id="assist_btn2" title="点亮屏幕" style="left: 27px; top: 152px;">
  <i class="iconfont" style="display:inline-block;color:red;width:20px;height:20px;">&#xe61d;</i>
</button>
<button class="assist-btn1 circle" id="assist_btn1" title="关闭屏幕亮度" style="left: 27px; top: 152px;">
  <i class="iconfont toc-title" style="display:inline-block;color:red;width:20px;height:20px;">&#xe61d;</i>
</button>


<script src="//cdn.bootcss.com/jquery/3.1.1/jquery.min.js"></script>	

<script src="https://my.openwrite.cn/js/readmore.js" type="text/javascript"></script>
<script>
  const btw = new BTWPlugin();
  btw.init({
    id: "container",
    blogId: "22699-1592137983091-414",
    name: "前端进阶之旅",
    qrcode: "https://poetries1.gitee.io/img-repo/2020/06/qrcode.jpg",
    keyword: "3a3b3c",
  });
</script>

<script type="text/javascript">

// white theme
var body = {color: "#555", background: "#000"};
var a_tag = {color: "#222"};
var header = { background: "#222"};
var logo_line_i = {background: "#222"};
// var post_code = {background: "#eee", color: "#222"};

function switch_theme() {
 $("body").css(body);
 $("a:not('.links-of-author-item a, .site-state-item a, .site-state-posts a, .feed-link a, .motion-element a, .post-tags a, .show-commit-cls a, #donate_board a')").css(a_tag);
 $(".header, .footer").css(header);
 $(".logo-line-before i, .logo-line-after i").css(logo_line_i);
 //$(".post code").css(post_code);
 $("#idhyt-surprise-ball #idhyt-surprise-ball-animation .drag").css(a_tag);
 $(".post-title-link, .posts-expand .post-meta, .post-comments-count, .disqus-comment-count, .post-category a, .post-nav-next a, .post-nav-item a").css(a_tag);
 
 // $("code").css({color: '#c5c8c6', background: '#1d1f21'});
 //$("#assist_btn1").hide(1500);
}

$(function () {
$("#assist_btn2").css("display","none");
 $("#assist_btn1").click(function() {
     switch_theme();
$("div#toc.toc-article").css({
 "background":"#eaeaea",
 "opacity":1
});
$(".toc-article ol").show();
$("#toc.toc-article .toc-title").css("color","#a98602");
$("#assist_btn1").css("display","none");
$("#assist_btn2").css("display","block");
 });
$("#assist_btn2").click(function() {
$("#assist_btn2").css("display","none");
$("#assist_btn1").css("display","block");
$("body").css("background","url(http://www.miaov.com/static/ie/images/news/bg.png)")
     $(".header, .footer").css("background","url(http://www.miaov.com/static/ie/images/news/bg.png)")
$(".toc-article ol").toggle(1000);
 });
});


//背景随机

var Y, O, E, L, B, C, T, z, N, S, A, I;
!function() {
var e = function() {
for (O.clearRect(0, 0, L, B), T = [{
x: 0,
y: .7 * B + C
}, {
x: 0,
y: .7 * B - C
}]; T[1].x < L + C;) t(T[0], T[1])
}, t = function(e, t) {
O.beginPath(), O.moveTo(e.x, e.y), O.lineTo(t.x, t.y);
var n = t.x + (2 * I() - .25) * C,
 r = a(t.y);
O.lineTo(n, r), O.closePath(), N -= S / -50, O.fillStyle = "#" + (127 * A(N) + 128 << 16 | 127 * A(N + S / 3) + 128 << 8 | 127 * A(N + S / 3 * 2) + 128).toString(16), O.fill(), T[0] = T[1], T[1] = {
 x: n,
 y: r
}
}, a = function n(e) {
var t = e + (2 * I() - 1.1) * C;
return t > B || t < 0 ? n(e) : t
};
Y = document.getElementById("evanyou"), O = Y.getContext("2d"), E = window.devicePixelRatio || 1, L = window.innerWidth, B = window.innerHeight, C = 90, z = Math, N = 0, S = 2 * z.PI, A = z.cos, I = z.random, Y.width = L * E, Y.height = B * E, O.scale(E, E), O.globalAlpha = .6, document.onclick = e, document.ontouchstart = e, e()
}()

   
$("#toc-eye").click(function(){
$("#toc.toc-article").toggle(1000);
});

</script>


   
  <div class="text-center donation">
    <div class="inner-donation">
      <span class="btn-donation">支持一下</span>
      <div class="donation-body">
        <div class="tip text-center">扫一扫，支持poetries</div>
        <ul>
        
          <li class="item">
            
              <span>微信扫一扫</span>
            
            <img src="/images/weixin.jpg" alt="">
          </li>
        
          <li class="item">
            
              <span>支付宝扫一扫</span>
            
            <img src="/images/zhifubao.jpg" alt="">
          </li>
        
        </ul>
      </div>
    </div>
  </div>


   
  <div class="box-prev-next clearfix">
    <a class="show pull-left" href="/2018/02/23/travis-ci/">
        <i class="icon icon-angle-left"></i>
    </a>
    <a class="show pull-right" href="/2018/02/25/linux-frequently-use-command/">
        <i class="icon icon-angle-right"></i>
    </a>
  </div>




</div>


  <a id="backTop" class="back-top">
    <i class="icon-angle-up"></i>
  </a>




  <div class="modal" id="modal">
  <span id="cover" class="cover hide"></span>
  <div id="modal-dialog" class="modal-dialog hide-dialog">
    <div class="modal-header">
      <span id="close" class="btn-close">关闭</span>
    </div>
    <hr>
    <div class="modal-body">
      <ul class="list-toolbox">
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/archives/"
              rel="noopener noreferrer"
              target="_self"
              >
              博客
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/categories/"
              rel="noopener noreferrer"
              target="_self"
              >
              分类
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/tags/"
              rel="noopener noreferrer"
              target="_self"
              >
              标签
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/search/"
              rel="noopener noreferrer"
              target="_self"
              >
              搜索
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/link/"
              rel="noopener noreferrer"
              target="_self"
              >
              友链
            </a>
          </li>
        
          <li class="item-toolbox">
            <a
              class="CIRCLE"
              href="/about/"
              rel="noopener noreferrer"
              target="_self"
              >
              关于
            </a>
          </li>
        
      </ul>

    </div>
  </div>
</div>



  
      <div class="fexo-comments comments-post">
    

    

    
    

    

    
    

    

<!-- Gitalk评论插件通用代码 -->
<div id="gitalk-container"></div>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.css">
<script src="https://cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.min.js"></script>
<script>
const gitalk = new Gitalk({
  clientID: '5567a2c4abb858009d96',
  clientSecret: 'b9039ec056cf5c2346b3cdb63308a28c163f91e5',
  repo: 'poetries.github.io',
  owner: 'poetries',
  // 在这里设置一下截取前50个字符串, 这是因为 github 对 label 的长度有了要求, 如果超过
  // 50个字符串则会报错.
  // id: location.pathname.split('/').pop().substring(0, 49),
  id: location.pathname,
  admin: ['poetries'],
  // facebook-like distraction free mode
  distractionFreeMode: false
})
gitalk.render('gitalk-container')
</script>
<!-- Gitalk代码结束 -->



  </div>

  

  <script type="text/javascript">
  function loadScript(url, callback) {
    var script = document.createElement('script')
    script.type = 'text/javascript';

    if (script.readyState) { //IE
      script.onreadystatechange = function() {
        if (script.readyState == 'loaded' ||
          script.readyState == 'complete') {
          script.onreadystatechange = null;
          callback();
        }
      };
    } else { //Others
      script.onload = function() {
        callback();
      };
    }

    script.src = url;
    document.getElementsByTagName('head')[0].appendChild(script);
  }

  window.onload = function() {
    loadScript('/js/bundle.js?235683', function() {
      // load success
    });
  }
</script>


  <!-- 页面点击小红心 -->
  <script type="text/javascript" src="/js/clicklove.js"></script>
 
  
</body>
</html>
